package com.youi.web;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.manage.HttpSessionManage;
import com.youi.common.DelStatus;
import com.youi.common.SessionKey;
import com.youi.service.UserorderexcelService;
import com.youi.third.ali.oss.OSSUtil;
import com.youi.entity.Sysuser;
import com.youi.entity.Userorderexcel;
import com.youi.common.Common;
import com.youi.util.CommonInfoUtil;
import com.youi.util.DateUtil;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.Page;

/**
* 订单导出历史控制层
* @author Administrator
*/
@Controller
public class UserorderexcelController  extends HttpRequestManage{

		@Autowired
		private UserorderexcelService userorderexcelService;

		

		/**
		 * I-126
		 * 查询订单导出历史
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/getUserorderexcel")
		public String getUserorderexcel(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{
			Sysuser sysuser=(Sysuser) HttpSessionManage.getAttribute(request, SessionKey.SYSUSER);
			Integer pageIndex=optInteger("pageIndex",request,1);
			Integer pageNum=optInteger("pageNum",request,10);
			Integer type=optInteger(Common.TYPE, request);
			String des=optString("des",request,Common.DESC);
			String pms=optString("pms",request);
			Date start=optDate("start",request,"yyyy-MM-dd HH:mm:ss");
			Date end=optDate("end",request,"yyyy-MM-dd HH:mm:ss");
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			prams.put("sysuserId", sysuser.getId());
			if (type!=null) {
				prams.put(Common.TYPE, type);
			}
			Map<String, Object> sortPram=new HashMap<String, Object>();
			sortPram.put(Common.ID,des);
			Map<String, Object> searchPram=null;
			if (pms!=null) {
				searchPram=new HashMap<String, Object>();
			}
			Map<String, Object> startTime=null;
			if(start!=null){
				startTime=new HashMap<String, Object>();
				startTime.put(Common.GMTCREATED,start);
			}
			Map<String, Object> endTime=null;
			if(end!=null){
				endTime=new HashMap<String, Object>();
				endTime.put(Common.GMTCREATED,end);
			}
			List<Userorderexcel> userorderexcelList=userorderexcelService.getByParamsList(prams, searchPram,sortPram,  (pageIndex-1)*pageNum, pageNum, startTime, endTime);
			if (userorderexcelList==null||userorderexcelList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			Integer pageCount=userorderexcelService.getCountByParamss(prams, searchPram, startTime, endTime);
			modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
			modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);			//pageIndex     查询页数
			modelMap.put(CommonInfoUtil.ALLCOUINT,pageCount);
			modelMap.put(CommonInfoUtil.DATA, userorderexcelList);		//jsonObjectList json对象集合
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}

		/**
		 * I-127
		 *查询订单导出excel下载地址
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/getUserorderexcelById")
		public String getUserorderexcelById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long id=getLong("id",request);
			Userorderexcel userorderexcel=userorderexcelService.get(id);
			if (userorderexcel==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			String objectName=userorderexcel.getPathUrl().substring(27,userorderexcel.getPathUrl().length());
			if (!OSSUtil.existObject(objectName)) {
				return JsonUtil.toJson(CommonInfoUtil.NULLOBJECT, "文件不存在请重新导出"); 
			}
			modelMap.put("url", userorderexcel.getPathUrl());
			modelMap.put(CommonInfoUtil.JSONCODE, CommonInfoUtil.SUCCESS);
			modelMap.put(CommonInfoUtil.JSONMSG, "操作成功");
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);//jsonObject	json对象
		}

		/**
		 * 查询所有订单导出历史
		 * @param modelMap
		 * @param session
		 * @return
		 */
		@ResponseBody
		@RequestMapping("getAllUserorderexcel")
		public String getAllUserorderexcel(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			List<Userorderexcel> userorderexcelList= userorderexcelService.getByParams(null);
			if (userorderexcelList==null||userorderexcelList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",userorderexcelList);//jsonObjectList	json对象集合
		}
		
		public static void main(String[] args) {
			String urlString="https://image.myjjk.com.cn/myj/miniapp/img/20200226/151643650.xls";
			System.out.println(urlString.substring(27,urlString.length()));
		}
}
